package de.bensoft.jrail.client.com;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.apache.log4j.Logger;

import de.bensoft.jrail.client.com.interfaces.LI100;

@Singleton
public class GlobalCommunicator {

	@Inject
	private LI100 li100;
	
	private static final Logger log = Logger.getLogger(GlobalCommunicator.class);
	
	private BooleanProperty trackPowerProperty;
	public BooleanProperty trackPowerProperty() { return trackPowerProperty; }
	
	public GlobalCommunicator() {
		trackPowerProperty = new SimpleBooleanProperty(false);
		
		trackPowerProperty.addListener(new ChangeListener<Boolean>() {
			public void changed(ObservableValue<? extends Boolean> arg0,Boolean arg1, Boolean arg2) {			
				if(arg2 != null && arg2.equals(Boolean.TRUE))
					commandResume();
				else 
					commandTrackPowerOff();					
			}		
		});
	}
	
	
	// **************************************************************************
	// COMMANDS
	// **************************************************************************
	public void commandTrackPowerOff() {
		log.info("Disable Trackpower");
		li100.sendCommand(LI100.INTERFACE_COMMAND_EMERGENCY_OFF);
	}
	
	public void commandResume() {
		log.info("Enable Trackpower");	
		li100.sendCommand(LI100.INTERFACE_COMMAND_RESUME);
	}
}
